package com.zzptc.app;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.DialogTitle;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zzptc.app.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Button btn_login;
    Button btn_reg;
    EditText et_name;
    EditText et_pwd;
    private static final String TAG = "MainActivity";
    List<User> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//实例化控件
        event();//处理事件
    }
    public void init(){
        btn_reg=findViewById(R.id.btn_reg);
        btn_login=findViewById(R.id.btn_login);
        et_name=findViewById(R.id.et_name);
        et_pwd=findViewById(R.id.et_pwd);
    }
    public void event(){
        //注册
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this, "9999", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&requestCode==3){
            //取值
            String uname =data.getStringExtra("uname");
            String pwd1  =data.getStringExtra("pwd1");
            Log.d(TAG,"回传的值是:"+uname);
            Log.d(TAG,"回传的值是:"+pwd1);
            //封装数据
            User user=new User(uname,pwd1);
            //注册
            reg(user);

        }
    }
    public void reg(User user) {
        //
        if (list.size()==0) {
            list.add(user);
            Log.d(TAG,list.size()+"==="+user.getUsername());
        } else {
            for (User s : list) {
                if (s.getUsername().equals(user.getUsername())){
                    Toast.makeText(this, "该用户已经存在", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    list.add(user);
                    Log.d(TAG,list.size()+"==="+user.getUsername());
                }
            }
            }
        }

    }